#==============================================================#
# File      :   pgbouncer.conf
# Desc      :   Pgbouncer Main Config for {{ pg_instance }}
# Time      :   {{ '%Y-%m-%d %H:%M' | strftime }}
# Host      :   {{ pg_instance }} @ {{ inventory_hostname }}:{{ pg_port }}
# Path      :   /etc/pgbouncer/pgbouncer.ini
# Note      :   ANSIBLE MANAGED, DO NOT CHANGE!
# License   :   AGPLv3 @ https://pigsty.io/docs/about/license
# Copyright :   2018-2025  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#
{% if pg_listen == '0.0.0.0' or pg_listen == '*' %}
{% set pg_listen_address = '*' %}
{% else %}
{% set pg_listen_address = pg_listen | replace('${ip}', inventory_hostname)  | replace('${lo}', '127.0.0.1') %}
{% if pg_vip_enabled|bool and not pg_vip_address.startswith('127.0.0') and '/' in pg_vip_address %}
{% set pg_listen_address = pg_listen_address | replace('${vip}', pg_vip_address.split('/')[0]) %}
{% endif %}
{% endif %}

[users]
%include /etc/pgbouncer/useropts.txt

[databases]
%include /etc/pgbouncer/database.txt

[pgbouncer]
pool_mode                   = {{ pgbouncer_poolmode }}
listen_addr                 = {{ pg_listen_address }}
listen_port                 = {{ pgbouncer_port }}
auth_type                   = hba
auth_file                   = /etc/pgbouncer/userlist.txt
auth_hba_file               = /etc/pgbouncer/pgb_hba.conf
{% if pgbouncer_auth_query|bool %}
auth_user                   = {{ pg_dbsu }}
auth_query                  = SELECT username, password FROM monitor.pgbouncer_auth($1)
{% endif %}
admin_users                 = {{ pg_dbsu }}, {{ pg_admin_username }}
stats_users                 = {{ pg_dbsu }}, {{ pg_monitor_username }}
server_reset_query          =
unix_socket_dir             = /var/run/postgresql
pidfile                     = /var/run/postgresql/pgbouncer.pid
logfile                     = {{ pgbouncer_log_dir }}/pgbouncer.log
application_name_add_host   = 1
max_client_conn             = 20000
min_pool_size               = 0
default_pool_size           = 32
reserve_pool_size           = 32
reserve_pool_timeout        = 1
server_lifetime             = 600
max_db_connections          = 100
max_user_connections        = 100
server_round_robin          = 0
log_connections             = 0
log_disconnections          = 0
max_prepared_statements     = 128
ignore_startup_parameters   = extra_float_digits
client_tls_sslmode          = {{ pgbouncer_sslmode }}
client_tls_cert_file        = /pg/cert/server.crt
client_tls_key_file         = /pg/cert/server.key
client_tls_ca_file          = /pg/cert/ca.crt
